package com.ymtms.arrivalofgoods.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Smsutil {
    /**
     * 新版本：阿里云云通信发送短信通知
     *
     * @param smsAppKey    TOP分配给应用的AppKey,即创建的签名
     * @param smsSerect    短信签名AppKey对应的secret值
     * @param smsSign      短信签名，传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名
     * @param smsModelId   短信模板ID，传入的模板必须是在阿里大于“管理中心-短信模板管理”中的可用模板。示例：SMS_585014
     * @param modelParam   模板内容里面的变量
     * @param phone        用户手机号码
     * @return boolean true成功false失败
     */
    public static boolean sendSms(String smsAppKey, String smsSerect, String smsSign, String smsModelId,   String phone,JSONObject modelParam) {
        //验证发送失败
        boolean result = false;
        //设置超时时间,可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化Client,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsAppKey, smsSerect);
        IAcsClient client = new DefaultAcsClient(profile);
        //组装请求对象
        CommonRequest request = new CommonRequest();
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("TemplateCode", smsModelId);
        request.putQueryParameter("SignName", smsSign);
        String smsParam = JSONObject.toJSONString(modelParam);
        request.putQueryParameter("TemplateParam", smsParam);

        try {
            CommonResponse response = client.getCommonResponse(request);

            System.out.println(response.getData());
            if ("ok".equals(response.getData())) {
                result=true;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }


        return result;
    }





}
